package com.baby.wheelview;

import com.net.framework.help.utils.StringUtil;

/**
 * @author zhangxiaobin
 * @version 1.1.0
 * @email zhangxiaobin@yipintech.com
 * @date 创建时间：2016年1月26日 上午9:13:25
 * @explain 年龄滚轮适配器装载
 */
public class AgeNumberWheelAdapter extends WheelAdapter {
    private String firstValue;
    private int startNumber;
    private int endNumber;
    private int interval;
    private String label;
    // 格式
    private String format;

    /**
     * @param startNumber 开始的数字
     * @param endNumber   结束的数字
     * @param interval    间隔
     * @param label       标记
     * @param format      格式
     */
    public AgeNumberWheelAdapter(String firstValue, int startNumber, int endNumber, int interval, String label, String format) {
        this.firstValue = firstValue;
        this.startNumber = startNumber;
        this.endNumber = endNumber;
        this.interval = interval;
        this.label = label;
        this.format = format;
    }

    /***
     * @param startNumber
     * @param endNumber
     * @param interval
     * @param format
     */
    public AgeNumberWheelAdapter(String firstValue, int startNumber, int endNumber, int interval, String format) {
        this(firstValue, startNumber, endNumber, interval, null, format);
    }

    @Override
    public int getCount() {
        return (endNumber - startNumber) / interval;
    }

    @Override
    public String getItem(int index) {
        String data;
        if (index == 0 && !StringUtil.isBlank(firstValue)) {
            data = firstValue;
        } else {
            int number = startNumber + index * interval;
            data = format != null ? String.format(format, number) : Integer.toString(number);
        }

        if (TextUtil.isEmpty(label))
            return data;

        return data + label;
    }

    /**
     * 设置起始数值
     *
     * @param startItem
     */
    public void setStartItem(int startItem) {
        this.startNumber = startItem * this.interval;
        this.notifyChanged();
    }

    @Override
    public int getValue(int index) {
        return index * this.interval + this.startNumber;
    }

    /**
     * 获取当前起始数字
     *
     * @return
     */
    @Override
    public int getStartValue() {
        return this.startNumber;
    }

    /**
     * 获取当前结束数字
     *
     * @return
     */
    @Override
    public int getEndValue() {
        return this.endNumber;
    }

    /**
     * 获取数字间隔
     *
     * @return
     */
    @Override
    public int getInterval() {
        return this.interval;
    }

    @Override
    public int getValueIndex(int value) {
        return (value - this.startNumber) / this.interval;
    }

    @Override
    public void setStartValue(int value) {
        this.startNumber = value;
    }
}